package com.linuxrm.countdown.gaokao
import android.service.dreams.DreamService
import android.view.Choreographer
import android.view.View
import android.widget.TextView
import java.util.*
class CountdownDreamService : DreamService() {
    private lateinit var tvDateTime: TextView
    private lateinit var tvCountdown: TextView
    private lateinit var frameCallback: Choreographer.FrameCallback
    private lateinit var contentContainer: View
    private lateinit var moveHandler: android.os.Handler
    private lateinit var moveRunnable: Runnable
    private lateinit var gaokaoDate: Calendar
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        setContentView(R.layout.activity_main)
        isInteractive = false
        findViewById<View>(R.id.btn_settings).visibility = View.GONE
        contentContainer = findViewById(R.id.content_container)
        contentContainer.scaleX = 0.75f
        contentContainer.scaleY = 0.75f
        tvDateTime = findViewById(R.id.tv_current_datetime)
        tvCountdown = findViewById(R.id.tv_countdown)
        gaokaoDate = CountdownLogic.getTargetDate(this)
        randomMoveContainer()
        moveHandler = android.os.Handler(mainLooper)
        moveRunnable = object : Runnable {
            override fun run() {
                randomMoveContainer()
                moveHandler.postDelayed(this, 15000)
            }
        }
        frameCallback = object : Choreographer.FrameCallback {
            override fun doFrame(frameTimeNanos: Long) {
                updateDisplay()
                Choreographer.getInstance().postFrameCallback(this)
            }
        }
    }
    private fun updateDaemonStatus() {
        val isRunning = CountdownLogic.isDaemonRunning(this)
        val tvRoot = findViewById<TextView>(R.id.tv_root)
        tvRoot.visibility = if (isRunning) View.GONE else View.VISIBLE
    }
    override fun onDreamingStarted() {
        super.onDreamingStarted()
        Choreographer.getInstance().postFrameCallback(frameCallback)
        moveHandler.post(moveRunnable)
        updateDaemonStatus()
    }
    override fun onDreamingStopped() {
        super.onDreamingStopped()
        Choreographer.getInstance().removeFrameCallback(frameCallback)
        moveHandler.removeCallbacks(moveRunnable)
    }
    private fun randomMoveContainer() {
        val rootContainer = findViewById<View>(R.id.root_container)
        if (rootContainer.width == 0 || rootContainer.height == 0) return
        val maxOffsetX = (rootContainer.width * 0.1).toInt()
        val maxOffsetY = (rootContainer.height * 0.1).toInt()
        val randomX = (-maxOffsetX..maxOffsetX).random()
        val randomY = (-maxOffsetY..maxOffsetY).random()
        val params = contentContainer.layoutParams as android.widget.FrameLayout.LayoutParams
        params.setMargins(randomX, randomY, 0, 0)
        contentContainer.layoutParams = params
    }
    private fun updateDisplay() {
        tvDateTime.text = CountdownLogic.getCurrentDateTimeString()
        tvCountdown.text = CountdownLogic.getCountdownText(gaokaoDate)
    }
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Choreographer.getInstance().removeFrameCallback(frameCallback)
        moveHandler.removeCallbacksAndMessages(null)
    }
}
